package cc.owoo.godpen.analysis.css;

import java.util.LinkedHashMap;

/**
 * CSS属性
 * Created by nimensei
 * 2022-05-30 下午 10:33
 */
public class Property {
    private final LinkedHashMap<String, String> property = new LinkedHashMap<>();// 属性

    /**
     * 解析样式属性表
     *
     * @param string 需要解析的样式属性表字符串
     * @return 样式属性表对象，如果解析结果为空或为null则返回null
     */
    public static Property parse(String string) {
        if (string == null || string.length() == 0)
            return null;
        StyleStringAnalysis analysis = new StyleStringAnalysis(string.toCharArray());
        Character character = analysis.nextNotNull(null);
        if (character == null)
            return null;
        char c = character;
        if (c != '{')
            analysis.reset();
        Property property = analysis.nextProperty('}');
        return property == null || property.size() == 0 ? null : property;
    }

    /**
     * 添加样式属性
     *
     * @param propertyName  属性名称
     * @param propertyValue 属性值
     */
    public void add(String propertyName, String propertyValue) {
        if (propertyName == null || (propertyName = propertyName.trim().toLowerCase()).length() == 0 ||
                propertyValue == null || (propertyValue = propertyValue.trim().toLowerCase()).length() == 0)
            return;
        property.put(propertyName, propertyValue);
    }

    /**
     * 获取属性值
     *
     * @param propertyName 属性名称
     * @return 属性值
     */
    public String get(String propertyName) {
        if (propertyName == null || (propertyName = propertyName.trim().toLowerCase()).length() == 0)
            return null;
        return property.get(propertyName);
    }

    /**
     * 获取样式属性数量
     *
     * @return 样式属性数量
     */
    public int size() {
        return property.size();
    }

    /**
     * 序列化样式属性
     *
     * @param string          序列化字符串
     * @param retract         缩进
     * @param format          是否格式化代码
     * @param parenthesisFree 是否不添加扩号
     */
    public void stringify(StringBuilder string, String retract, boolean format, boolean parenthesisFree) {
        if (property.size() == 0) {
            string.append("{}");
            return;
        }
        boolean valid = !parenthesisFree;
        if (valid)
            string.append('{');
        for (var entry : property.entrySet()) {
            if (format) {
                if (valid)
                    string.append('\n');
                else
                    valid = true;
                if (retract != null)
                    string.append(retract);
                if (!parenthesisFree)
                    string.append("    ");
            }
            string.append(entry.getKey()).append(':');
            if (format)
                string.append(' ');
            string.append(entry.getValue()).append(';');
        }
        if (!parenthesisFree) {
            if (format)
                string.append('\n');
            string.append('}');
        }
    }

    /**
     * 将样式属性序列化
     *
     * @return 序列化后的字符串
     */
    public String stringify() {
        StringBuilder string = new StringBuilder();
        stringify(string, null, false, false);
        return string.toString();
    }

    /**
     * 将样式属性序列化，并进行格式化处理
     *
     * @return 序列化后的字符串
     */
    public String stringifyFormat() {
        StringBuilder string = new StringBuilder();
        stringify(string, null, true, false);
        return string.toString();
    }

    /**
     * 将样式属性序列化，并不带有括号
     *
     * @return 序列化后的字符串
     */
    public String stringifyParenthesisFree() {
        StringBuilder string = new StringBuilder();
        stringify(string, null, false, true);
        return string.toString();
    }

    @Override
    public String toString() {
        return stringify();
    }
}
